package com.zero.service;

import com.zero.entity.Admin;
import com.zero.entity.Family;
import com.zero.entity.FamilyMember;
import com.zero.mapper.AdminMapper;
import com.zero.mapper.FamilyMapper;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.util.JWTUtil;
import com.zero.util.Loginer;
import com.zero.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class LoginService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private FamilyMapper familyMapper;

    @Autowired
    private WebService webService;

    /**
     * 登录逻辑
     *
     * @param loginer
     * @return
     */
    public Result login(Loginer loginer) {
        if (loginer == null)
            return Result.fail("用户信息不完整");
        if (loginer.getAccount() == null || "".equals(loginer.getAccount()))
            return Result.fail("登录帐号为空");
        if (loginer.getPassword() == null || "".equals(loginer.getPassword()))
            return Result.fail("登录密码为空");

        //更新访问量
        webService.updateWebView();

        //管理员登录
        if (loginer.getType() == 0) {
            Admin admin = new Admin();
            admin.setAccount(loginer.getAccount());
            admin.setPassword(loginer.getPassword());
            List<Admin> admins = adminMapper.select(admin);
            if (admins.size() == 1) {

                //生成token
                String token = JWTUtil.sign(admins.get(0));
                HashMap<String, Object> map = new HashMap<>();
                map.put("token", token);
                map.put("admin", admins.get(0));

                return Result.ok(0,"登录成功", map);
            }
            return Result.fail("帐号密码不匹配");

        } else if (loginer.getType() == 1) {
            FamilyMember familyMember = new FamilyMember();
            familyMember.setAccount(loginer.getAccount());
            familyMember.setPassword(loginer.getPassword());
            List<FamilyMember> familyMembers = familyMemberMapper.select(familyMember);

            if (familyMembers.size() == 1) {
                //生成token
                String token = JWTUtil.sign(familyMembers.get(0));

//                System.out.println("---------家庭成员生成的token----------");
//                System.out.println(token);
//                System.out.println("-------------------");

                HashMap<String, Object> map = new HashMap<>();
                map.put("token", token);
                map.put("member", familyMembers.get(0));

                return Result.ok(2,"登录成功", map);
            }
            return Result.fail("帐号密码不匹配");
        }

        return Result.fail("未知错误");
    }

    /**
     * 注册逻辑
     *
     * @param familyMember
     * @return
     */
    @Transactional
    public Result register(FamilyMember familyMember) {
        //判断数据完整性
        if (familyMember == null)
            return Result.fail("注册信息不完整");

        System.out.println(familyMember);
        //查询该帐号是否已被注册
        FamilyMember param = new FamilyMember();
        param.setAccount(familyMember.getAccount());
        List<FamilyMember> result = familyMemberMapper.select(param);

        //已被注册则返回提示信息
        if (result.size() > 0)
            return Result.fail("该帐号已被注册，请使用其他帐号");

        //否则，先创建一个家庭
        Family family = new Family();
        family.setName(familyMember.getFamilyName());
        family.setCreateDate(new Date());
        family.setDebt(new BigDecimal(0));
        family.setAssets(new BigDecimal(0));
        familyMapper.add(family);

        //再创建一个家庭用户
        familyMember.setFamilyId(family.getId());
        familyMember.setCreateDate(new Date());
        familyMember.setAssets(new BigDecimal(0));
        familyMember.setDebt(new BigDecimal(0));
        familyMemberMapper.add(familyMember);

        boolean flag = webService.updateWebCount();
        log.info("更新家庭数量和用户数量的操作：" + flag);

        return Result.ok("用户注册成功，请返回进行登录");
    }

}